package com.example.sharding.demo.controller;

import com.example.sharding.demo.model.BaseEntity;
import com.example.sharding.demo.model.table.User;
import com.example.sharding.demo.service.IUserService;
import com.example.sharding.demo.utils.DateUtil;
import com.example.sharding.demo.utils.KeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liulanhua on 2018/9/25.
 */
@RestController
public class IndexController {

    @Autowired
    IUserService userService;

    @GetMapping(value = "/insertUser")
    public ModelAndView insertUser(User user, ModelAndView mv) {
        int id = (int)(Math.random() * 1000000000d);
        String userId = KeyGenerator.getUUID();
        user.setId(id);
        user.setName("11");
        user.setPassword("11");
        user.setUserId(userId);
        user.setGender(1);
        user.setCreateTime(new Date());
        int i = userService.insertUser(new BaseEntity<User>(user));
        mv.setViewName("index");
        mv.addObject("record", "Hello, Please login !");
        return mv;
    }

    @GetMapping(value = "/getUser")
    public Map<String,Object> getUser(User user, ModelAndView mv) {
        Map<String,Object> result = new HashMap<>();
        BaseEntity be = new BaseEntity<User>(user);
        be.setQueryStartTime(DateUtil.str2Date("2020-05-01 00:00:00","yyyy-MM-dd HH:mm:ss"));
        be.setQueryEndTime(DateUtil.str2Date("2020-08-20 00:00:00","yyyy-MM-dd HH:mm:ss"));
        List<User> list = userService.findUserList(be);
        result.put("data",list);
        return result;
    }

}
